<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="55px">
      <el-form-item label="医生" prop="docKey">
        <!-- <el-input v-model="queryParams.docKey" placeholder="请输入医生" clearable size="small"
          @keyup.enter.native="handleQuery" /> -->
        <el-select v-model="queryParams.docKey" clearable size="small">
          <el-option v-for="item in dockerList" :key="item.docKey" :label="item.docName" :value="item.docKey" />
        </el-select>
      </el-form-item>
      <el-form-item label="症状" prop="symptom">
        <el-input v-model="queryParams.symptom" placeholder="请输入症状" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="病名" prop="bingName">
        <el-input v-model="queryParams.bingName" placeholder="请输入病名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="时间" prop="times">
        <!-- <el-date-picker clearable size="small" v-model="queryParams.time" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择时间">
        </el-date-picker> -->
        <el-date-picker v-model="queryParams.times" type="daterange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['zhongyao:introduce:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['zhongyao:introduce:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['zhongyao:introduce:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['zhongyao:introduce:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="introduceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="key" />
      <el-table-column label="医生" align="center" prop="docName" />
      <el-table-column label="药剂" align="center" prop="prescriptionName" />
      <el-table-column label="患者" align="center" prop="username" />
      <el-table-column label="症状" align="center" prop="symptom" />
      <el-table-column label="介绍" align="center" prop="introduce">
        <template slot-scope="scope">
          <el-popover placement="bottom" width="800" trigger="click" :content="introduce">
            <el-button size="medium" type="text" icon="el-icon-view" slot="reference"
              @click="openNodeHtml(scope.row.introduce)"></el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['zhongyao:introduce:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['zhongyao:introduce:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改医案维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="医生" prop="docKey">
          <!-- <el-input v-model="form.docKey" placeholder="请输入医生" /> -->
          <el-select v-model="form.docKey" clearable size="small">
            <el-option v-for="item in dockerList" :key="item.docKey" :label="item.docName" :value="item.docKey" />

          </el-select>
        </el-form-item>
        <el-form-item label="患者" prop="username">
          <el-input v-model="form.username" placeholder="请输入医生" />

        </el-form-item>
        <el-form-item label="药剂" prop="prescriptionKey">
          <!-- <el-input v-model="form.prescriptionKey" placeholder="请输入药剂" /> -->
          <el-select v-model="form.prescriptionKey" clearable size="small">
            <el-option v-for="item in prescriptionList" :key="item.prescriptionKey" :label="item.prescriptionName"
              :value="item.prescriptionKey" />

          </el-select>
        </el-form-item>
        <el-form-item label="症状" prop="symptom">
          <el-input v-model="form.symptom" placeholder="请输入症状" />
        </el-form-item>
        <el-form-item label="病类" prop="bingName">
          <el-select v-model="form.bingName" clearable size="small">
            <el-option v-for="item in bingList" :key="item.value" 
              
              :value="item.value" :label="item.name"/>

          </el-select>
        </el-form-item>

        <el-form-item label="时间" prop="time">
          <el-date-picker clearable size="small" v-model="form.time" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="介绍" prop="introduce">
          <editor v-model="form.introduce" placeholder="请输入介绍" :min-height="142" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listIntroduce, getIntroduce, delIntroduce, addIntroduce, updateIntroduce } from "@/api/zhongyao/introduce";
import { listDocker } from "@/api/zhongyao/docker";
import { listPrescription } from "@/api/zhongyao/prescription";
export default {
  name: "Introduce",
  data() {
    return {
      bingList:[
        {
          value:1,
          name:"呼吸科"
        } ,   {
          value:2,
          name:"消化科"
        }  ,      {
          value:3,
          name:"肾内科"
        }  ,  {
          value:4,
          name:"妇产科"
        },
        {
          value:5,
          name:"心内科"
        },
        {
          value:6,
          name:"肿瘤科"
        }
      ],
      // 遮罩层
      times:[],
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 医案维护表格数据
      introduceList: [],
      prescriptionList: [],
      dockerList: [],
      // 弹出层标题
      title: "",
      introduce: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        docKey: null,
        symptom: null,
        bingId: null,
        time: null,
        times: []
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        docKey: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        prescriptionKey: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        symptom: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        introduce: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询医师维护列表 */
    openNodeHtml(introduce) {
      this.introduce = introduce.replace(/<[^>]+>/g, "");
      this.visible = true
    },
    //移除HTML标签
    delTag(str) {
      return str.replace(/<[^>]+>/g, "");
    },
    /** 查询医案维护列表 */
    getList() {
      this.loading = true;
      listIntroduce(this.queryParams).then(response => {
        this.introduceList = response.rows;
        this.total = response.total;
        this.loading = false;
    
      });
      listDocker().then(response => {
        this.dockerList = response.rows;
      });
      listPrescription().then(response => {
        this.prescriptionList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        key: null,
        docKey: null,
        prescriptionKey: null,
        symptom: null,
        bingName: null,
        introduce: null,
        time: null,
        createTme: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        deleteFlag: null,
   
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
     
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.key)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加医案维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const key = row.key || this.ids
      getIntroduce(key).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改医案维护";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.key != null) {
            updateIntroduce(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIntroduce(this.form).then(response => {
              console.log(this.form)
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const keys = row.key || this.ids;
      this.$modal.confirm('是否确认删除医案维护编号为"' + keys + '"的数据项？').then(function () {
        return delIntroduce(keys);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zhongyao/introduce/export', {
        ...this.queryParams
      }, `introduce_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
